package boids
{
	import flash.display.Sprite;
	import flash.geom.Vector3D;

	public class BoidUnit extends Sprite
	{
		
		private static var THRUST_FORCE:Number 				= 3000.0;
		private static var MAX_THRUST:Number 				= 4000.0;
		private static var MIN_THRUST:Number 				= 0.0;
		private static var D_THRUST:Number 					= 0.01;
		private static var LINEAR_DRAG_COEFFICIENT:Number	= 10.5;
		private static var ANGULAR_DRAG_COEFFICIENT:Number 	= 2000.0;
		private static var STEERING_FORCE:Number 			= 2000.0;
		
		private var pThrust:Vector3D= new Vector3D();
		private var sThrust:Vector3D = new Vector3D();
		private var vForces:Vector3D = new Vector3D();
		private var vMoment:Vector3D = new Vector3D();
				
		public var mass:uint		= 10;
		public var intertia:uint	= 10;
		public var inertiaInverse:uint = 1/intertia;
		public var isLeader:Boolean = false;
		
		public var orientation:Number;
		public var thrustForce:Number;
		public var interceptor:Boolean; 
		
		public function BoidUnit(isInterceptor:Boolean)
		{
			this.interceptor = isInterceptor;
			init();
		}
		
		public function setThrusters(p:Boolean, s:Boolean, f:Number):void
		{
			pThrust.x = 0;
			pThrust.y = 0;
			sThrust.x = 0;
			sThrust.y = 0;
			
			if(p)
				pThrust.x = -STEERING_FORCE * f;
			if(s)
				sThrust.x = STEERING_FORCE * f;	
				
		}
		
		public function updateBodyEuler(dt:Number):void
		{
			var a:Vector3D = new Vector3D();
			var dv:Vector3D = new Vector3D();
			var ds:Vector3D = new Vector3D();
			var aa:Number = new Number();
			var dav:Number = new Number();
			var dr:Number = new Number();
			
			calculateLoads();
			
			
			
		}
		
		public function modulateThrust(up:Boolean):void
		{
			var dT:Number = up ? D_THRUST : -D_THRUST ;
			thrustForce += dT;
			
			if(thrustForce > MAX_THRUST)
				thrustForce = MAX_THRUST;
				
			if(thrustForce < MIN_THRUST)
				thrustForce = MIN_THRUST;
				
		}
		
		private function calculateLoads():void
		{
			var fB:Vector3D = new Vector3D();				// stores the sum of forces
			var mB:Vector3D = new Vector3D();				// stores the sum of moments
			var thrust:Vector3D = new Vector3D();			// thrust vector
	
			// reset forces and moments:
			vForces.x = 0.0;
			vForces.y = 0.0;
			vForces.z = 0.0;	// always zero in 2D
		
			vMoment.x = 0.0;	// always zero in 2D
			vMoment.y = 0.0;	// always zero in 2D
			vMoment.z = 0.0;
			
			thrust.x = 0.0;
			thrust.y = 1.0;
			thrust.z = 0.0;  // zero in 2D
			thrust.scaleBy(THRUST_FORCE);
			
			// Calculate forces and moments in body space:
			var vLocalVelocity:Vector3D = new Vector3D();
			var vResultant:Vector3D = new Vector3D();	
			var vtmp:Vector3D = new Vector3D();	
			var vDragVector:Vector3D = new Vector3D();	
			var fLocalSpeed:Number = new Number();
			var tmp:Number = new Number();

			
		}
		
		private function init():void
		{
			this.orientation = Math.random()*360;
			
			if(interceptor)
				this.thrustForce = THRUST_FORCE * 1.5;
			else
				this.thrustForce = THRUST_FORCE;				
			
			drawBoid();	
		}
		
		private function drawBoid():void
		{
			this.graphics.lineStyle(2,0xff00ff);
			this.graphics.drawRect(-10,-10,20,20);
		}		
	}
}